﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TheVoiceOfSnake.Models;

namespace TheVoiceOfSnake
{
    public class Frame
    {
        //private int N;  //długość ramki
        //private int M;  // przesunięcie
        private double[] table;
        private List<Complex> complexValues;

        public Frame()
        {
            complexValues = new List<Complex>();
        }

        public Frame(byte[] tab)
            : this()
        {
            ConvertFromByteArray(tab);
        }

        public double this[int index]
        {
            get { return table[index]; }
            set { table[index] = value; }
        }

        public static implicit operator double[](Frame frame)
        {
            double[] table = new double[frame.table.Length];

            for (int i = 0; i < frame.table.Length; i++)
            {
                table[i] = frame.table[i];
            }
            return table;
        }

        public static explicit operator Frame(byte[] table)
        {
            Frame frame = new Frame();
            frame.table = new double[table.Length];

            for (int i = 0; i < table.Length; i++)
            {
                frame.table[i] = Convert.ToDouble(table[i]) - 128;
            }
            return frame;
        }

        public void ConvertFromByteArray(byte[] table)
        {
            this.table = new double[table.Length];

            for (int i = 0; i < table.Length; i++)
            {
                this.table[i] = Convert.ToDouble(table[i]) - 128;
            }
        }

        public void ConvertFromDoubleArray(double[] table)
        {
            this.table = table;
        }

        public int Size()
        {
            return table.Length;
        }

        public void FillComplexValues(double[] real, double[] imag)
        {
            for (int i = 0; i < real.Length; i++)
            {
                complexValues.Add(new Complex(real[i], imag[i]));
            }
        }

        public double GetRealPart(int index)
        {
            return complexValues[index].Real;
        }

        public double GetImagPart(int index)
        {
            return complexValues[index].Imag;
        }

        public double[] GetTableOfValues()
        {
            return table;
        }

        public void SetTableOfValues(IEnumerable<double> tab)
        {
            table = new double[tab.Count()];
            for (int i = 0; i < tab.Count(); i++)
            {
                table[i] = tab.ElementAt(i);
            }
        }
    }
}
